package cc.git.liuyan.customeraiagent.core.embeddingmodel.abstrct;

import cc.git.liuyan.customeraiagent.core.embeddingmodel.EmbeddingModel;
import cc.git.liuyan.customeraiagent.core.embeddingmodel.EmbeddingModelApiClient;
import cc.git.liuyan.customeraiagent.core.embeddingmodel.EmbeddingModelProvider;
import cc.git.liuyan.customeraiagent.core.embeddingmodel.EmbeddingModelProviderInfo;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Supplier;

public abstract class AbstractEmbeddingModelProvider implements EmbeddingModelProvider {
    private final EmbeddingModelProviderInfo providerInfo;
    protected final EmbeddingModelApiClient apiClient;
    private final Map<String, EmbeddingModel> embeddingModelMap = new ConcurrentHashMap<>();

    public AbstractEmbeddingModelProvider(EmbeddingModelProviderInfo providerInfo, Supplier<EmbeddingModelApiClient> embeddingApiClientSupplier) {
        this.providerInfo = providerInfo;
        this.apiClient = embeddingApiClientSupplier.get();
    }

    @Override
    public final EmbeddingModelProviderInfo getProviderInfo() {
        return this.providerInfo;
    }

    @Override
    public final EmbeddingModel getEmbeddingModel(String modelName) {
        if (this.embeddingModelMap.containsKey(modelName)) {
            return this.embeddingModelMap.get(modelName);
        } else {
            EmbeddingModel embeddingModel = this.createEmbeddingModel(modelName);
            this.embeddingModelMap.put(modelName, embeddingModel);
            return embeddingModel;
        }
    }

    protected abstract EmbeddingModel createEmbeddingModel(String modelName);

}
